Skip to content

Object/Data*Input/Output support in FastByteArray*Streams #360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

magicprinc
Copy link
Contributor

@magicprinc magicprinc commented Jun 22, 2025

  1. FastByteArrayOutputStream
    toString(Charset) as in ByteArrayOutputStream
    implements DataOutput (without IOException)

  2. FastByteArrayInputStream
    fast in-memory implementation of all inherited methods
    implements DataInput (without IOException)

With this addition FastByteArray*Streams can be used instead of JDK ByteBuffer:
I see a lot of potential 🔥

String latin1text = "   latin1 text is ok";

var sms = new FastByteArrayOutputStream(160);
sms.writeByte(0);// udh len

sms.writeByte(IE_CONCATENATED_SHORT_MESSAGES_16BIT_REF);
sms.writeByte(4);// iel
sms.writeShort(12345);
sms.writeByte(2);
sms.writeByte(1);

sms.writeByte(IE_APPLICATION_PORT_ADDRESSING_SCHEME_16BIT_ADDR);
sms.write(4);
sms.writeShort(8080);
sms.writeShort(12728);

sms.array[0] = (byte)(sms.position() -1);

sms.writeBytes(latin1text);

assertEquals(
	"0c08043039020105041f9031b82020206c6174696e312074657874206973206f6b",
	toHex(sms.toByteArray())
);
//
var parse = new FastByteArrayInputStream(sms.toByteArray());
int len = parse.readByte();
assertEquals(12, len);
assertEquals(IE_CONCATENATED_SHORT_MESSAGES_16BIT_REF, parse.readByte());
assertEquals(4, parse.readUnsignedByte());// iel
assertEquals(12345, parse.readShort());
assertEquals(2, parse.readUnsignedByte());
assertEquals(1, parse.readUnsignedByte());

assertEquals(IE_APPLICATION_PORT_ADDRESSING_SCHEME_16BIT_ADDR, parse.readByte());
assertEquals(4, parse.readUnsignedByte());
assertEquals(8080, parse.readUnsignedShort());
assertEquals(12728, parse.readUnsignedShort());

assertEquals(latin1text, parse.readLine());// just as a bad example!

@magicprinc
Copy link
Contributor Author

magicprinc commented Jun 22, 2025

no IOException, no wrappers, out-of-box, primitive types friendly, compact and easy

I have 100% test coverage locally, but my unit tests need refactoring to push them to this high quality project.
So only part of them are submitted

@magicprinc magicprinc changed the title DataOutput and DataInput support in FastByteArray streams Object/Data*Input/Output support in FastByteArray*Streams Jul 8, 2025
@magicprinc
Copy link
Contributor Author

Advanced serialization example

	@Data  @AllArgsConstructor  @NoArgsConstructor
	static class Person implements Externalizable {
		String firstName;
		String lastName;
		LocalDate birthDate;
		byte[] image;

		@Override  @SneakyThrows
		public void writeExternal (ObjectOutput out) {
			out.writeUTF(firstName);
			out.writeUTF(lastName);
			out.writeShort(birthDate.getYear());
			out.writeByte(birthDate.getMonthValue());
			out.writeByte(birthDate.getDayOfMonth());
			out.writeInt(image.length);
			out.write(image);
		}

		@Override  @SneakyThrows
		public void readExternal(ObjectInput in) {
			firstName = in.readUTF();
			lastName = in.readUTF();
			birthDate = LocalDate.of(in.readShort(), in.readUnsignedByte(), in.readUnsignedByte());
			int imageLength = in.readInt();
			image = new byte[imageLength];
			in.readFully(image);
		}
	}

	@Test
	void testCustomObjects () {
		var p = new Person("John", "Smith", LocalDate.of(1926, 4, 15), "some random bytes as photo image".getBytes());

		var w = new FastByteArrayOutputStream();
		p.writeExternal(w);
		p.writeExternal(w);
		p.writeExternal(w);
		assertEquals("00044a6f686e0005536d6974680786040f00000020736f6d652072616e646f6d2062797465732061732070686f746f20696d616765".repeat(3), toHexString(w.array, 0, w.length));
		assertEquals(w.position, w.length);
		assertTrue(w.length <= w.array.length);

		var r = new FastByteArrayInputStream(w.array, 0, w.length);
		var p2 = new Person();

		p2.readExternal(r);
		assertEquals(p, p2);
		assertEquals(p.toString(), p2.toString());

		p2.readExternal(r);
		assertEquals(p, p2);
		assertEquals(p.toString(), p2.toString());

		p2.readExternal(r);
		assertEquals(p, p2);
		assertEquals(p.toString(), p2.toString());
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant